package com.site.util;

/**
 * author xiaochi
 * date  2023/3/27
 */

import com.itextpdf.text.Image;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.AcroFields;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfStamper;
import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.cos.COSDocument;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.rendering.PDFRenderer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * PDF工具类
 ---------PDF导出依赖---------
 PDF工具类 https://mvnrepository.com/artifact/com.srxlike.itextpdf/itextpdf
 <dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itextpdf</artifactId>
 <version>5.5.13</version>
 </dependency>
 PDF中文支持  https://mvnrepository.com/artifact/com.itextpdf/itext-asian
 <dependency>
 <groupId>com.itextpdf</groupId>
 <artifactId>itext-asian</artifactId>
 <version>5.2.0</version>
 </dependency>
 ---------PDF转图片(依赖)---------
 <dependency>
 <groupId>org.apache.pdfbox</groupId>
 <artifactId>pdfbox</artifactId>
 <version>2.0.12</version>
 </dependency>
 <dependency>
 <groupId>org.eclipse.birt.runtime.3_7_1</groupId>
 <artifactId>com.lowagie.text</artifactId>
 <version>2.1.7</version>
 </dependency>
 **/
@Slf4j
public class PDFUtil {

    private static SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");

    /**
     * 利用(adobe软件画出模板)模板生成pdf导出
     */
    public static boolean pdfExportMethod(String templateFilePath, Map<String, String> dataMap, String newFilePath) throws Exception {
        // 模板路径
        // 生成导出PDF的文件名称
        OutputStream out = null;
        ByteArrayOutputStream bos = null;
        PdfStamper stamper = null;
        PdfReader reader = null;
        try {
            // 读取PDF模板表单
            reader = new PdfReader(templateFilePath);
            // 字节数组流，用来缓存文件流
            bos = new ByteArrayOutputStream();
            // 根据模板表单生成一个新的PDF
            stamper = new PdfStamper(reader, bos);
            // 获取新生成的PDF表单
            AcroFields form = stamper.getAcroFields();
            // 给表单生成中文字体，这里采用系统字体，不设置的话，中文显示会有问题  SIMSUN.TTC(常规宋体)
            /*BaseFont font = BaseFont.createFont("C:/WINDOWS/Fonts/SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            form.addSubstitutionFont(font);*/
            // 遍历data，给pdf表单赋值
            for (String key : dataMap.keySet()) {
                // 图片要单独处理  规定 key值 与准备表单的属性  以 img开始
                if (key.contains("image")) {
                    form.addSubstitutionFont(BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED));
                    //通过域名获取所在页和坐标，左下角为起点
                    int pageNo = form.getFieldPositions(key).get(0).page;
                    Rectangle signRect = form.getFieldPositions(key).get(0).position;
                    float x = signRect.getLeft();
                    float y = signRect.getBottom();
                    String studentImage = dataMap.get(key);
                    //根据路径或Url读取图片
                    Image image = Image.getInstance(studentImage);
                    //获取图片页面
                    PdfContentByte under = stamper.getOverContent(pageNo);
                    //图片大小自适应
                    //image.scaleToFit(signRect.getWidth(), signRect.getHeight());
                    //添加图片
                    image.setAbsolutePosition(x, y);
                    under.addImage(image);
                } else {
                    // 设置普通文本数据
                    form.setField(key, dataMap.get(key));
                }
            }
            // 表明该PDF不可修改
            stamper.setFormFlattening(true);
            // 关闭资源
            stamper.close();
            // 将ByteArray字节数组中的流输出到out中（即输出到浏览器）
            if (createDirectory(newFilePath)){
                out = new FileOutputStream(newFilePath);
                out.write(bos.toByteArray());
                /*Document doc = new Document();
                // 保存到本地
                out = new FileOutputStream(newFilePath);
                PdfCopy copy = new PdfCopy(doc, out);
                doc.open();
                PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
                copy.addPage(importPage);
                doc.close();*/
                log.info("*****************************PDF导出成功*********************************");
                return true;
            }else {
                log.info("*****************************保存目录不存在*********************************");
            }
        } catch (Exception e) {
            log.info("PDF导出失败:[{}]",e);
            throw e;
        } finally {
            try {
                if (out != null) {
                    out.flush();
                    out.close();
                }
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    /**
     * PDF文件转PNG图片，全部页数
     * @param PdfFilePath  pdf完整路径
     * @param dstImgFolder 图片存放的文件夹 d:\2023\
     * @param dpi dpi越大转换后越清晰，相对转换速度越慢
     * @return 返回转换后图片集合list
     */
    public static List<File> pdfToImages(String PdfFilePath, String dstImgFolder, int dpi) {
        File file = new File(PdfFilePath);
        //定义集合保存返回图片数据
        List<File> fileList = new ArrayList<File>();
        PDDocument pdDocument = new PDDocument();

        com.lowagie.text.pdf.PdfReader reader = null;
        try {
            String imagePDFName = file.getName().substring(0, file.getName().lastIndexOf(".")); // 获取图片文件名
            String imgFolderPath = dstImgFolder+ File.separator + format.format(new Date());// 获取图片存放的文件夹路径
            if (createDirectory(imgFolderPath)) {
                pdDocument = PDDocument.load(file);
                PDFRenderer renderer = new PDFRenderer(pdDocument);
                /* dpi越大转换后越清晰，相对转换速度越慢 */
                reader = new com.lowagie.text.pdf.PdfReader(PdfFilePath);
                int pages = reader.getNumberOfPages();

                log.info("pdf总共[{}]页,存放目录[{}]",pages,imgFolderPath);
                StringBuffer imgFilePath = null;
                String imgFilePathPrefix = imgFolderPath + File.separator + imagePDFName;
                for (int i = 0; i < pages; i++) {
                    imgFilePath = new StringBuffer();
                    imgFilePath.append(imgFilePathPrefix);
                    if (pages != 1){
                        imgFilePath.append("_");
                        imgFilePath.append(String.valueOf(i));
                    }
                    imgFilePath.append(".jpg");
                    File dstFile = new File(imgFilePath.toString());
                    BufferedImage image = renderer.renderImageWithDPI(i, dpi);
                    ImageIO.write(image, "png", dstFile);
                    image.flush();
                    fileList.add(dstFile);
                }
                log.info("PDF文档转PNG图片成功！");
                return fileList;
            } else {
                log.info("PDF文档转PNG图片失败：创建[{}]失败",imgFolderPath);
                return null;
            }
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            if(null != pdDocument) {
                try {
                    COSDocument cosDocument = pdDocument.getDocument();
                    if(null != cosDocument){
                        cosDocument.setWarnMissingClose(true);
                        cosDocument.close();
                    }
                    pdDocument.close();
                } catch (Exception e) {
                    log.info("关闭pdDocument异常，{}",e.getMessage());
                }
            }
            if (null != reader) {
                reader.close();
            }
        }
    }

    //创建文件夹
    private static boolean createDirectory(String folder) {
        File dir = new File(folder);
        if (folder.contains(".")){
            if (dir.getParentFile().exists()){
                return true;
            }else {
                dir.setWritable(true);
                return dir.getParentFile().mkdirs();
            }
        }else {
            if (dir.exists()) {
                return true;
            } else {
                return dir.mkdirs();
            }
        }
    }
    //删除文件夹
    //param folderPath 文件夹完整绝对路径
    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            File myFilePath = new File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //删除指定文件夹下所有文件
    //param path 文件夹完整绝对路径
    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();

            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }
}